#include "StdAfx.h"
#include "sa_TwMeshFile.h"


#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480


sa_TwMeshFile::sa_TwMeshFile(void):
meshSpaceship(0)
,material(0)
,numMaterials(0)
{
}

sa_TwMeshFile::~sa_TwMeshFile(void)
{
}
void sa_TwMeshFile::InitGraphics()
{
	LPD3DXBUFFER bufShipMaterial;

	D3DXLoadMeshFromX((LPCSTR)"spaceship 2.x",    // load this file
		D3DXMESH_SYSTEMMEM,    // load the mesh into system TwMemory
		g_Render->GetDevice(),    // the Direct3D Device
		NULL,    // we aren't using adjacency
		&bufShipMaterial,    // put the materials here
		NULL,    // we aren't using effect instances
		&numMaterials,    // the number of materials in this model
		&meshSpaceship);    // put the mesh here

	// retrieve the pointer to the buffer containing the material information
	D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();

	// create a new material buffer for each material in the mesh
	material = new D3DMATERIAL9[numMaterials];

	for(DWORD i = 0; i < numMaterials; i++)    // for each material...
	{
		material[i] = tempMaterials[i].MatD3D;    // get the material info
		material[i].Ambient = material[i].Diffuse;    // make ambient the same as diffuse
	}
}

void sa_TwMeshFile::InitRenderState()
{
	g_Render->GetDevice()->SetRenderState(D3DRS_LIGHTING, TRUE);    // turn on the 3D lighting
	g_Render->GetDevice()->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
	g_Render->GetDevice()->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    // ambient light
}

void sa_TwMeshFile::InitLight()
{
	D3DLIGHT9 light;    // create the light struct

	ZeroMemory(&light, sizeof(light));    // clear out the struct for use
	light.Type = D3DLIGHT_DIRECTIONAL;    // make the light type 'directional light'
	light.Diffuse.r = 0.5f;    // .5 red
	light.Diffuse.g = 0.5f;    // .5 green
	light.Diffuse.b = 0.5f;    // .5 blue
	light.Diffuse.a = 1.0f;    // full alpha (we'll get to that soon)

	D3DVECTOR vecDirection = {-1.0f, -0.3f, -1.0f};    // the direction of the light
	light.Direction = vecDirection;    // set the direction

	g_Render->GetDevice()->SetLight(0, &light);    // send the light struct properties to light #0
	g_Render->GetDevice()->LightEnable(0, TRUE);    // turn on light #0


}

void sa_TwMeshFile::Update()
{

}

void sa_TwMeshFile::Render()
{
	g_Render->GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
	g_Render->GetDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	g_Render->GetDevice()->BeginScene();

	// SET UP THE TRANSFORMS

	D3DXMATRIX matView;    // the view transform matrix
	D3DXMatrixLookAtLH(&matView,
		&D3DXVECTOR3 (0.0f, 4.0f, 8.0f),    // the camera position
		&D3DXVECTOR3 (0.0f, 0.0f, 0.0f),    // the look-at position
		&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
	g_Render->GetDevice()->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView 

	D3DXMATRIX matProjection;    // the projection transform matrix
	D3DXMatrixPerspectiveFovLH(&matProjection,
		D3DXToRadian(45),    // the horizontal field of view
		SCREEN_WIDTH / SCREEN_HEIGHT,    // the aspect ratio
		1.0f,    // the near view-plane
		100.0f);    // the far view-plane
	g_Render->GetDevice()->SetTransform(D3DTS_PROJECTION, &matProjection);    // set the projection

	static float index = 0.0f; index+=0.03f;    // an ever-increasing float value
	D3DXMATRIX matRotateY;    // a matrix to store the rotation for each triangle
	D3DXMatrixRotationY(&matRotateY, index);    // the rotation matrix
	g_Render->GetDevice()->SetTransform(D3DTS_WORLD, &(matRotateY));    // set the world transform


	// draw the spaceship
	for(DWORD i = 0; i < numMaterials; i++)    // loop through each subset
	{
		g_Render->GetDevice()->SetMaterial(&material[i]);    // set the material for the subset
		meshSpaceship->DrawSubset(i);    // draw the subset
	}

	g_Render->GetDevice()->EndScene(); 

	g_Render->GetDevice()->Present(NULL, NULL, NULL, NULL);


}

void sa_TwMeshFile::Clear()
{
	meshSpaceship->Release();    // close and release the spaceship mesh

}